# coding = utf-8
# Author = Ann
# Date: 2023/6/27


import json
import os
import csv

import openpyxl
from tools.reader.config_reader import ConfigReader


def element_reader():
    """
    Read element positioning within a JSON file
    """
    all_elements_dict = []
    for file_name in file_type_matcher("elements", "classpath", "json"):
        with open(file_name, "r") as file:
            data_dict = json.load(file)
            all_elements_dict.extend(data_dict)
    return all_elements_dict


def secret_reader():
    secret_dict = []
    for i in file_type_matcher("data", "classpath", "json"):
        full_file_name = ConfigReader().read("data")["classpath"] + "/" + i
        with open(full_file_name, "r") as file:
            data_dict = json.load(file)
            secret_dict.extend(data_dict)
    return secret_dict


def excel_reader(file_name, sheet_name):
    """
    Read the data inside the Excel file
    """
    try:
        full_file_name = ConfigReader().read("data")["classpath"] + "/" + file_name
        is_exist = os.path.exists(full_file_name)
        if is_exist:
            workbook = openpyxl.load_workbook(full_file_name)
            sheet = workbook[sheet_name]
            # rows Get all the cells in the form by row, and the data of each row is placed in a progenitor
            res = list(sheet.rows)
            # Get the data of the first row in the excel table as the key -> dictionary to generate a list list
            title = [i.value for i in res[0]]
            # as a container for each dictionary
            cases = []
            # Iterate through all but the first row
            for item in res[1:]:
                # Get the data for each row
                data_line = [i.value for i in item]
                # Pack each row of traversed data with the first line of title data into a dictionary
                dic_line = dict(zip(title, data_line))
                cases.append(dic_line)
            return cases
    except Exception as e:
        raise e


def csv_reader(file_name):
    data_list = []
    full_file_name = ConfigReader().read("data")["classpath"] + "/" + file_name
    with open(full_file_name, 'r') as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            data_list.append(row)
    return data_list


def file_type_matcher(project_name, project_value, file_type):
    """
    Look for files of the same type and store them in the list
    :param project_name: The name of the project in the ini file
    :param project_value: The path to the project in the ini file
    :param file_type: The type of file looking for
    """
    match_filename_list = []
    file_path = ConfigReader().read(project_name)[project_value]
    for root, dirs, files in os.walk(file_path):
        # Find the file that meets the requirements from all the files that have been traversed
        if files:
            for file_name in files:
                if file_name.endswith(file_type):
                    path = os.path.join(root, file_name)
                    match_filename_list.append(path)
    if match_filename_list is None:
        raise TypeError("No matching file format!")
    return match_filename_list
